#include<iostream>
#include<unistd.h>
#include<signal.h>
#include<cstdlib>
#include<sys/wait.h>

void WaitAll(int num)
{
    while (true)
    {
        //sleep(10);
        pid_t n = waitpid(-1, nullptr, WNOHANG); // 阻塞了！
        if (n == 0)
        {
            
            break;
        }
        else if (n < 0)
        {
            std::cout << "waitpid error " << std::endl;
            break;
        }
        std::cout << "father get a signal: " << num << std::endl;
    }
    
}
//void WaitAll(int num)
//{
//    std::cout << "father get a signal: " << num << std::endl;
//}
int main()
{
    signal(SIGCHLD,WaitAll);
    for(int i = 1; i <= 10; i++)
    {
        pid_t id = fork();
        if(id == 0)
        {
            std::cout << "I am child , exit" << std::endl;
            sleep(5);
            exit(3);
        } 
    }
    while(true)
    {
        //waitpid(id,nullptr,0);
        std::cout << "I am father , exit" << std::endl;
        sleep(10);
    }
    return 0;
}
